﻿using System.Xml;

namespace LdtJudge.DataEngine.Common.XmlAssemble
{
    /// <summary>
    /// XML数据的操作
    /// </summary>

    public  class HandleXmlData
    {
        /// <summary>
        /// 根据当前节点，返回xmlpath
        /// </summary>
        /// <param name="xmlNode">节点名称</param>
        /// <returns></returns>
        public static string GetXmlFullPath(XmlNode xmlNode)
        {
            try
            {
                string path = "/" + xmlNode.Name;
                XmlNode parent = xmlNode.ParentNode;
                //循环查找节点的父路径直至到顶级无父路径 则为全路径
                while (parent != null && parent != xmlNode.OwnerDocument)
                {
                    path = "/" + parent.Name + path;
                    parent = parent.ParentNode;
                }
                path = path.TrimStart('/');
                return path;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 添加序号
        /// </summary>
        /// <param name="xmlDocument">需要添加的DOC</param>
        /// <returns></returns>
        public static XmlDocument AddSerialNumber(XmlDocument xmlDocument)
        {
            //NodeSerialNumber(xmlDocument.FirstChild, xmlDocument);

            XmlNode nodeRoot = xmlDocument.FirstChild;
            string path = nodeRoot.Name;
            foreach (XmlNode xmlNode in nodeRoot.ChildNodes)
            {
                NodelNumber(xmlNode.Name, nodeRoot, xmlDocument);

            }
            return xmlDocument;
        }

        private static void NodelNumber(string path,XmlNode xmlNode,XmlDocument xmlDocument) 
        {
           XmlNodeList nodeList= xmlNode.SelectNodes(path);
            for (int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList.Count>1)
                {
                    if (nodeList[i].Attributes["序号"] == null)
                    {
                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("序号");
                        xmlAttribute.Value = i.ToString();
                        nodeList[i].Attributes.Append(xmlAttribute);
                    }
                }
               
                foreach (XmlNode node in nodeList[i].ChildNodes)
                {
                    string pathch = node.Name;
                    NodelNumber(pathch, nodeList[i], xmlDocument);
                }
            }
        
        }
        /// <summary>
        /// 添加节点序号
        /// </summary>
        /// <param name="xmlNode">节点</param>
        /// <param name="xmlDocument">需要添加DOC</param>
        private static void NodeSerialNumber(XmlNode xmlNode, XmlDocument xmlDocument)
        {
            if (xmlNode.ChildNodes.Count > 0)
            {
                bool addNumber = false;
                if (xmlNode.ChildNodes.Count > 1)
                {
                    addNumber = true;
                }
                for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
                {
                    if (addNumber)
                    {
                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("序号");
                        xmlAttribute.Value = i.ToString();
                        xmlNode.Attributes.Append(xmlAttribute);
                    }

                    NodeSerialNumber(xmlNode.ChildNodes[i], xmlDocument);
                }
            }
        }


        public static string SelectPath(string path,XmlNode node) 
        {
            path = path + "/" + node.Name;
            return path;
        }

    }
}
